package service;

import java.util.List;

import bean.Cart;
import bean.CartRowMapper;
import common.BaseDAO;

public class CartDAO extends BaseDAO{
	//根据用户id查找所有购物车商品
	public List<Cart> queryByUserid(int userid){
		String sql="select * from cart_info where userid=?";
		return this.query(sql, new CartRowMapper(), userid);
	}
	
	//根据用户id和商品id查找购物车商品
	public List<Cart> queryByUseridAndGoodsid(int userid,int goodsid){
		String sql="select * from cart_info where userid=? and goodsid=?";
		return this.query(sql, new CartRowMapper(), userid,goodsid);
	}
	
	//根据用户id和商品id添加购物车商品数量
	public void addGoods(int userid,int goodsid, int num){
		String sql="insert into cart_info(userid,goodsid,goods_num) values(?,?,?)";
		this.update(sql, userid,goodsid,num);
	}
	
	//根据用户id和商品id改变购物车对应商品的数量
	public void updateNum(int userid,int goodsid, int num){
		String sql="update cart_info set goods_num=? where userid=? and goodsid=?";
		this.update(sql, num,userid,goodsid);
	}
	
	//根据用户id和商品id删除购物车对应商品
	public void deleteGoods(int userid,int goodsid){
		String sql="delete from cart_info where userid=? and goodsid=?";
		this.update(sql, userid,goodsid);
	}
	
	//根据用户id清空购物车对应商品
	public void deleteAllGoods(int userid){
		String sql="delete from cart_info where userid=?";
		this.update(sql, userid);
	}
}
